package com.id.coinscleaner.junk

import android.util.Log
import androidx.core.os.bundleOf
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.id.coinscleaner.R
import com.id.coinscleaner.adapters.CleanAdapter
import com.id.coinscleaner.common.ViewBindingFragment
import com.id.coinscleaner.databinding.FragmentJunkShowBinding
import com.id.coinscleaner.viewmodel.JunkViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class JunkShowFragment : ViewBindingFragment<FragmentJunkShowBinding>() {

    val viewModel by lazy { ViewModelProvider(this.requireActivity()).get(JunkViewModel::class.java) }

    val adapter by lazy { CleanAdapter(this) }


    override fun initView() {
        super.initView()
//        lifecycleScope.launchWhenResumed {
//            viewModel.initData()
//        }
        adapter.initAdapterData(viewModel.data)
        Log.w(javaClass.name, "JunkshowFragment.initView -> ${viewModel.data}")
        binding.cleanRv.layoutManager = LinearLayoutManager(this.requireActivity())
        binding.cleanRv.adapter = adapter
        binding.cleanBtn.setOnClickListener {
            lifecycleScope.launch {
                var finishNum: String? = null
                if (viewModel.currentPreGarbageNum.value != null && viewModel.currentPreGarbageUnit.value != null) {
                    finishNum =
                        viewModel.currentPreGarbageNum.value + viewModel.currentPreGarbageUnit.value
                }
                val bundle = bundleOf("finish_num" to finishNum)
                navigate(R.id.action_junkShowFragment_to_junkBoostFragment,bundle)
            }
        }
        binding.finishBtn.setOnClickListener {
            this.requireActivity().finish()
        }
    }


}